package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.PurchaseDao;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseDao purchaseDao;


    @Override
    public void savePurchaseList(PurchaseList purchaseList, String purchaseListGoodsStr) {
        Gson gson = new Gson();
        this.purchaseDao.savePurchaseList(purchaseList);
        List<Map<String, Object>> list = gson.fromJson(purchaseListGoodsStr, List.class);
        if (CollectionUtils.isEmpty(list)) {
            for (Map<String, Object> map : list) {
                PurchaseListGoods purchase = new PurchaseListGoods();
                //split(".")[0] 得到的是第一个.之前的内容
                purchase.setGoodsId(Integer.parseInt(map.get("goodsId").toString().split("\\.")[0]));
                purchase.setGoodsCode((String) map.get("goodsCode"));
                purchase.setGoodsName((String) map.get("goodsName"));
                purchase.setGoodsModel((String) map.get("goodsModel"));
                purchase.setGoodsUnit((String) map.get("goodsUnit"));
                purchase.setGoodsNum(Integer.parseInt((String) map.get("goodsNum")));
                Integer price = Integer.parseInt(map.get("price").toString().split("\\.")[0]);
                Integer total = Integer.parseInt(map.get("total").toString().split("\\.")[0]);
                purchase.setPurchaseListId(purchaseList.getPurchaseListId());
                purchase.setGoodsTypeId(Integer.parseInt(map.get("goodsTypeId").toString().split("\\.")[0]));
                purchaseDao.savePurchaseListGoods(purchase, price, total);
            }
        }
    }

    @Override
    public Map<String, Object> getPurchaseList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<PurchaseList> lists = purchaseDao.getPurchaseList(purchaseNumber, supplierId, state, sTime, eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", lists);
        return map;
    }

    @Override
    public Map<Object, Object> getPurchaseListGoods(Integer purchaseListId) {
        List<PurchaseListGoods> purchaseListGoodsList = purchaseDao.getPurchaseListGoods(purchaseListId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rows",purchaseListGoodsList);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void purchaseDelete(Integer purchaseListId) {

        //删除进货单对应的商品信息
        purchaseDao.purchaseDeleteGoods(purchaseListId);
        //删除进货单
        purchaseDao.purchaseDelete(purchaseListId);

    }

    //更改支付状态
    @Override
    public void updateState(Integer purchaseListId) {
        this.purchaseDao.updateState(purchaseListId);
    }
    @Override
    public List<PurchaseList> getCount(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PurchaseList> getPurchaseList = purchaseDao.getPurchaseCount(sTime,eTime,goodsTypeId,codeOrName);
        return getPurchaseList;
    }
}